<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="序号" prop="sortNum">
        <el-input-number v-model="dataForm.sortNum" placeholder="序号"></el-input-number>
      </el-form-item>
      <el-form-item label="代码" prop="code">
      <el-input v-model="dataForm.code" placeholder="代码"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="控制点ID" prop="groupId">
      <el-input v-model="dataForm.groupId" placeholder="控制点ID"></el-input>
    </el-form-item>
    <el-form-item label="数据读取方式" prop="dataReadType">
      <el-input v-model="dataForm.dataReadType" placeholder="数据读取方式"></el-input>
    </el-form-item>
    <el-form-item label="计算规则" prop="expression">
      <el-input v-model="dataForm.expression" placeholder="计算规则"></el-input>
    </el-form-item>

    <el-form-item label="是否必需" prop="isRequired">
      <el-radio v-model="dataForm.required" label="1">是</el-radio>
      <el-radio v-model="dataForm.required" label="0">否</el-radio>
    </el-form-item>

    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {checkOptionGetObj,checkOptionAddObj,checkOptionPutObj} from '@/api/def/checkOption'
    export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          code: '',
          name: '',
          groupId: '',
          sortNum: '',
          dataReadType: '',
          expression: '',
          htmlContent: '',
          jsonContent: '',
          preCheck:false,
            required: false
        },
        dataRule: {
          code: [
            { required: true, message: '代码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          groupId: [
            { required: true, message: '控制点ID不能为空', trigger: 'blur' }
          ],
          sortNum: [
            { required: true, message: '序号不能为空', trigger: 'blur' }
          ],
          dataReadType: [
            { required: true, message: '数据读取方式不能为空', trigger: 'blur' }
          ],
          expression: [
            { required: true, message: '计算规则不能为空', trigger: 'blur' }
          ],
          htmlContent: [
            { required: true, message: 'html内容不能为空', trigger: 'blur' }
          ],
          jsonContent: [
            { required: true, message: 'json内容不能为空', trigger: 'blur' }
          ],
          isRequired: [
            { required: true, message: '是否必需不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            checkOptionGetObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                checkOptionPutObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                checkOptionAddObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      }
    }
  }
</script>
